package cn.chencaiju.day11;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class DataLoad {
	public static final String url="E:\\camp\\camp2018\\day10\\scores.csv";
//	public static Object dataLoad(String file) {
//		File source=new File(file);
//		if(!source.exists())
//			return null;
//		bufferedReader(file, "gbk");
//		return null;
//	}
	
	/**
	 * 以指定的编码格式读取数据并返回
	 * @param file
	 * @param charsetName
	 * @return 以Map数据类型返回
	 */
	public static Map<String,Map<String,Integer>> bufferedReader(String file,String charsetName) {
		
		Map<String,Map<String,Integer>> data=new HashMap<String,Map<String,Integer>>();
		try(
				BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(file),charsetName))
			)
		{
			StringBuilder sb=new StringBuilder();
			String temp=br.readLine();
			while(temp!=null&&!temp.isEmpty()) {
				sb.append(temp);
				sb.append('\n');
				temp=br.readLine();
			}
			String[] contents=sb.toString().split("[,\n]");
			Map<String,Integer> scores1=new HashMap<>();
			Map<String,Integer> scores2=new HashMap<>();
			//记录姓名，分数，课程
			for(int i=0;(7+i*5)<contents.length;i++) {
				if(contents[6+i*5].trim().equals("线性代数")) {
					scores1.put(contents[7+i*5], Integer.valueOf(contents[5+i*5]));
					data.put(contents[6+i*5], scores1);
				}else if(contents[6+i*5].trim().equals("高等数学（1）")){
					scores2.put(contents[7+i*5], Integer.valueOf(contents[5+i*5]));
					data.put(contents[6+i*5], scores2);
				}
//				System.out.println(contents[i*5+7]+"\t"+contents[6+i*5]+"\t"+contents[5+i*5]);
				
			}
			return data;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Test public void test() {
		Map<String,Map<String,Integer>> data=new HashMap<String,Map<String,Integer>>();
		data=bufferedReader(url,"gbk");
		Set<Entry<String, Map<String, Integer>>> entrySet = data.entrySet();
		for(Entry<String, Map<String,Integer>> item:entrySet) {
			System.out.print(item.getKey()+"\t");
			Set<Entry<String, Integer>> entrySet2 = item.getValue().entrySet();
			for(Entry<String,Integer> temp:entrySet2) {
				System.out.println(temp.getKey()+"\t"+temp.getValue());
			}
		}
	}
}
